﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class EditPromotion : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 DataTable dt = new DataTable();
            dt = GetData();
            ASPxHtmlEditor1.Html = dt.Rows[0]["Promotion"].ToString();
            }
           
          
        }
        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [ID],[Promotion] FROM [TB_Promotion] ORDER BY ID DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        protected void bt_confirm_Click(object sender, EventArgs e)
        {
                InsertPromotion();
        }
        public static object GetIsNull(object value)
        {
            if (value == null || value == "")
            {
                return DBNull.Value;
            }

            return value;
        }
        private void InsertPromotion()
        {
            string sqltext = @"insert into TB_Promotion ([ID],[Promotion]) values (@ID,@Pro)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlcon = new SqlConnection(Connection);
            sqlcomm.Connection = sqlcon;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@ID", GetIsNull(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Pro", GetIsNull(ASPxHtmlEditor1.Html));
            
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
        }
        
    }
}